﻿using M_ApiResult;
using M_Purchase.Api.Appliction.Command.AllocateCommand;
using M_Purchase.Domain.DTO;
using M_Purchase.Domain;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using Microsoft.IdentityModel.JsonWebTokens;

namespace M_Purchase.Api.Controllers
{
    /// <summary>
    /// 调拨控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AllocateController : ControllerBase
    {
        /// <summary>
        /// Mediator
        /// </summary>
        protected readonly IMediator _mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_mediator">Mediator</param>
        public AllocateController(IMediator _mediator)
        {
            this._mediator = _mediator;
        }

        /// <summary>
        /// 新增调拨管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreadAllocateInfo(CreatedAllocateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                //command.sub = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value); //获取用户Id,
                //command.sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取企业Id,
                //command.jti = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value;//获取系统Id,
                //command.CreatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取名称
                //command.Reviewer = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//审核人
                result.Code = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 调拨管理列表分页展示
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<AllocateDto<Allocate>>> ShowAllocateList([FromQuery] ShowAllocateCommand command)
        {
            ApiResult<AllocateDto<Allocate>> result = new ApiResult<AllocateDto<Allocate>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除调拨管理信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteAllocate(DeletedAllocateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填调拨管理信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Allocate>> ReverseFillingAllocate([FromQuery] ReverseFillingAllocateCommand command)
        {
            ApiResult<Allocate> result = new ApiResult<Allocate>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改调拨管理信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateAllocate(UpdateAllocateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改调拨管理状态
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateAllocateStatus(UpdateStatusAllocateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        } 
    }
}
